package com.xiaoyu.client.ui.activity.help;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.xiaoyu.client.R;
import com.xiaoyu.client.model.PicParams;
import com.xiaoyu.commonlib.ui.base.BaseActivity;
import com.xiaoyu.commonlib.utils.StatusBarUtil;
import com.xiaoyu.commonlib.utils.TounChImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jx
 * @date 创建时间 2018/8/13
 * @Description
 */
public class ChooseSchedulePicActivity extends BaseActivity {
    private TextView mCertain;
    private ViewPager mViewPager;
    private ImageView[] mImageViews;
    private List<PicParams.ContentBean> iconContent;//上一页面传过来的有关日程背景图的list
    private String[] mImgs = new String[]{};//将图片放入string数组中
    private int position; //每张图片的下标
    private String url; //选中的图片的url

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_schedule_pic);
        StatusBarUtil.StatusBarLightMode(this);
        setOccupyViewLinearGradient();
        initView();
        getIntentData();
    }


    private void initView() {
        mViewPager = findViewById(R.id.activity_choose_schedule_pic_view_pager);
        mCertain = findViewById(R.id.activity_choose_schedule_pic_certain);
    }

    //获取上一页面的值
    private void getIntentData() {
        position = getIntent().getExtras().getInt(HelpDetailActivity.POSITION);
        iconContent = (List<PicParams.ContentBean>) getIntent().getSerializableExtra(HelpDetailActivity.PIC_ICON);


        Log.i("jx", "getIntentData: 1----------" + position);
        Log.i("jx", "getIntentData: 2----------" + iconContent);
        //-------------------------------------------------
        //将所有图片放入list集合中，再将其转换成string数组
        List<String> mList = new ArrayList<>();
        for (int i = 0; i < iconContent.size(); i++) {
            mList.add(iconContent.get(i).getPicUrl());
        }
        //使用toArray(T[] a)方法
        mImgs = mList.toArray(new String[mList.size()]);
        //-------------------------------------------------

        mImageViews = new ImageView[mImgs.length];


        ViewPagerAdapter mAdapter = new ViewPagerAdapter();
        mViewPager.setAdapter(mAdapter);


        //---------------viewPager设置第几个图片以及滑动时显示的是第几张图片----------------------------
        mViewPager.setCurrentItem(position, false);
        //     跳转进ImageView时的序号，第几个图片/总个数
        mCertain.setText(position + 1 + "/" + iconContent.size());

        url = iconContent.get(position).getPicUrl();

        //滑动ViewPager的时候，根据外界的数据改变当前的选中状态和当前的图片的位置描述文本
        mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                mCertain.setText(position + 1 + "/" + iconContent.size());

                url = iconContent.get(position).getPicUrl();
            }
        });

    }


    class ViewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mImgs.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            //可以使用其他的ImageView 控件
            TounChImageView tounChImageView = new TounChImageView(ChooseSchedulePicActivity.this);
            try {
                Glide.with(ChooseSchedulePicActivity.this).load(mImgs[position])
//                        .placeholder(R.mipmap.app_guide_page1)//默认显示的图片
//                         .resize(500,300)//控制图片高度，不添加则自适应
//                        .error(R.mipmap.app_guide_page1)//加载时出现错误显示的图片
                        .into(tounChImageView);
            } catch (Exception e) {

            }
            //单击返回
            tounChImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });

            container.addView(tounChImageView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            mImageViews[position] = tounChImageView;
//            mImageViews[position].setScaleType(ImageView.ScaleType.FIT_XY);
            return tounChImageView;
        }


        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mImageViews[position]);
        }
    }

}
